package com.dataseek.iot.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.device.entity.DeviceAttrValue;
import com.dataseek.iot.device.entity.DeviceAttrValueHis;
import com.dataseek.iot.device.entity.PdtAttrRel;
import com.dataseek.iot.device.entity.PdtAttributeDic;
import com.dataseek.iot.device.mapper.DeviceAttValueHisMapper;
import com.dataseek.iot.device.mapper.DeviceAttValueMapper;
import com.dataseek.iot.device.mapper.PdtAttrRelMapper;
import com.dataseek.iot.device.mapper.PdtAttributeDicMapper;
import com.dataseek.iot.device.service.DeviceAttValueService;
import com.dataseek.iot.device.vo.DeviceAttValueVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.service.IDServiceHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @discription:
 * @author: Li
 * @date: 2022/1/11
 */
@Service
@Slf4j
public class DeviceAttValueServiceImpl implements DeviceAttValueService {
    @Autowired
    private IDServiceHelper idServiceHelper;

    @Autowired
    private DeviceAttValueMapper deviceAttValueMapper;

    @Autowired
    private PdtAttrRelMapper pdtAttrRelMapper;

    @Autowired
    private PdtAttributeDicMapper pdtAttributeDicMapper;

    @Autowired
    private DeviceAttValueHisMapper deviceAttValueHisMapper;

    @Override
    public DeviceAttValueVo queryDeviceAttList(String deviceCode, String attCode, Integer num, Integer size) {
        DeviceAttValueVo deviceAttValueVo = new DeviceAttValueVo();
        QueryWrapper<DeviceAttrValue> qw = new QueryWrapper<>();
        if (deviceCode != null && !deviceCode.isEmpty()) {
            qw.eq("device_code", deviceCode);
        }
        if (attCode != null && !attCode.isEmpty()) {
            qw.eq("att_code", attCode);
        }
        IPage<DeviceAttrValue> page = new Page<>(num, size);
        List<DeviceAttrValue> mapList = deviceAttValueMapper.selectPage(page,qw).getRecords();
        deviceAttValueVo.setPage(num);
        deviceAttValueVo.setSize(size);
        deviceAttValueVo.setTotal(page.getTotal());
        deviceAttValueVo.setList(mapList);
        return deviceAttValueVo;
    }
    @Override
    public List<DeviceAttrValue> queryDeviceAttList(String deviceCode, String attCode) {
        DeviceAttValueVo deviceAttValueVo = new DeviceAttValueVo();
        QueryWrapper<DeviceAttrValue> qw = new QueryWrapper<>();
        if (deviceCode != null && !deviceCode.isEmpty()) {
            qw.eq("device_code", deviceCode);
        }
        if (attCode != null && !attCode.isEmpty()) {
            qw.eq("att_code", attCode);
        }
        qw.orderByAsc("att_code");
        List<DeviceAttrValue> mapList = deviceAttValueMapper.selectList(qw);
        return mapList;
    }
    //新增设备属性
    @Override
    public Integer insertDeviceAtt(DeviceAttrValue deviceAttrValue) {
        int resultCode = 0;
        //查询是否是相同设备code存在
        String code = deviceAttrValue.getDeviceCode();
        String pdtCode=deviceAttrValue.getPdtCode();//产品code
        QueryWrapper<DeviceAttrValue> qw = new QueryWrapper<>();
        qw.eq("device_code", code);
        Integer count = deviceAttValueMapper.selectCount(qw);
        if (count == 0) {
            //关联设备属性
            //根据
            Map pdtAttMap=new HashMap();
            pdtAttMap.put("pdt_code",pdtCode);
            List<PdtAttrRel> pdtAttList= pdtAttrRelMapper.selectByMap(pdtAttMap);
           // pdtAttributeDic.setTenantCode(tenantCode);
            for(PdtAttrRel pdtAttrRel:pdtAttList){
                long id = idServiceHelper.nextId();
                deviceAttrValue.setId(id);
                deviceAttrValue.setAttCode(pdtAttrRel.getAttCode());
                //查询属性字典属性
                    String attrCode = pdtAttrRel.getAttCode();
                    QueryWrapper<PdtAttributeDic> qwAttr = new QueryWrapper<>();
                    qwAttr.eq("att_code", attrCode);
                    PdtAttributeDic pdtAttributeDic = pdtAttributeDicMapper.selectOne(qwAttr);
                deviceAttrValue.setAttName(pdtAttributeDic.getAttName());
                deviceAttrValue.setAttType(pdtAttributeDic.getAttType());
                deviceAttrValue.setDataType(pdtAttributeDic.getDataType());
                deviceAttrValue.setReadFlag(pdtAttributeDic.getReadFlag());
                deviceAttValueMapper.insert(deviceAttrValue);
            }
        } else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;

    }

    @Override
    public Integer updateDeviceAtt(DeviceAttrValue deviceAttrValue) {
        int resultCode = deviceAttValueMapper.updateById(deviceAttrValue);
        return resultCode;
    }

    @Override
    public Integer deleteDeviceAtt(String deviceCode) {
        //根据条件删除
        Map<String,Object> map = new HashMap<>();
        map.put("device_code",deviceCode);
        Integer resultCode =deviceAttValueMapper.deleteByMap(map);
        return resultCode ;
    }

    @Override
    public Integer insertDeviceAttHis(DeviceAttrValueHis deviceAttrValueHis) {
        long hisId=idServiceHelper.nextId();
        deviceAttrValueHis.setId(hisId);
        Integer resultCode= deviceAttValueHisMapper.insert(deviceAttrValueHis);
        return resultCode;
    }

    /**
     * 根据设备code/属性/日期查询设备历史情况
     * @param deviceCode
     * @param attCode
     * @param queryDay
     * @return
     */
    @Override
    public List<DeviceAttrValueHis> queryDeviceAttHisList(String deviceCode, String attCode, String queryDay) {

        QueryWrapper<DeviceAttrValueHis> qw = new QueryWrapper<>();
        if (deviceCode != null && !deviceCode.isEmpty()) {
            qw.eq("device_code", deviceCode);
        }
        if (attCode != null && !attCode.isEmpty()) {
            qw.eq("att_code", attCode);
        }
        if (queryDay != null && !queryDay.isEmpty()) {
            qw.like("crete_time", queryDay);
        }
        List<DeviceAttrValueHis> deviceAttrValueHisList=deviceAttValueHisMapper.selectList(qw);
        return deviceAttrValueHisList;
    }

}
